﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Runtime.CompilerServices;
using System.Text;

namespace PIM_ATE
{
    public class MicroDBTransaction : IDisposable
    {//DB事务支持
      
        public IsolationLevel IsolationLevel { get; private set; }
        public string ConnectionAliasName { get; private set; }
        /// <summary>
        /// 标记事务是否结束
        /// </summary>
        private bool IsTranEnded = false;
        /// <summary>
        /// 标记执行是否顺利
        /// </summary>
        private bool IsSuccess = false;

        #region 数据库对象
        internal SqlConnection connection;
        internal SqlTransaction tran;
        #endregion

        #region 事务用途
        /// <summary>
        /// 事务用途
        /// </summary>
        [Obsolete("Thinking only and it's not be used now.", true)]
        internal enum TransactionPurpose: byte
        {
            ReadOnly,
            ReadWrite,
        }
        
        #endregion

        private MicroDBTransaction()
        {}
        internal MicroDBTransaction(IsolationLevel isolationLevel, 
                                    string connectionAliasName = MicroDBHelper.ALIAS_NAME_DEFAULT)
        {
            this.IsolationLevel         = isolationLevel;
            this.ConnectionAliasName    = connectionAliasName;
        }


        #region IDisposable

        public bool IsDisposed { get; private set; }    
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        private void Dispose(bool disposing)
        {
            if (disposing)
            {
                //结束事务
                EndTransaction();
                //释放托管资源
                DisposeManagedResource();
            }         
            //释放非托管资源
            IsDisposed = true;
        }

        /// <summary>
        /// 释放托管资源
        /// </summary>
        [MethodImpl(MethodImplOptions.Synchronized)]
        private void DisposeManagedResource()
        {
            if (tran != null)
            {
                tran.Dispose();
                tran = null;
            }
            if (connection != null)
            {
                connection.Dispose();
                connection = null;
            }
        }

        ~MicroDBTransaction()
        {
            Dispose(false);
        }
       
        #endregion


        #region 使其就绪
        /// <summary>
        /// 使其就绪
        /// </summary>
        /// <returns>就绪与否</returns>        
        internal bool MakeReady()
        {
            try
            {
                ConnectionAndCreateTran();

                return true;
            }
            catch (Exception)
            {
                this.Dispose();
                return false;
            }
        }

        /// <summary>
        /// 连接数据库并创建事务
        /// </summary>
        [MethodImpl(MethodImplOptions.Synchronized)]
        private void ConnectionAndCreateTran()
        {
            this.connection = new SqlConnection( MicroDBHelper.GetConnection(ConnectionAliasName) );
            connection.Open();
            this.tran       = connection.BeginTransaction(IsolationLevel);
        }

        #endregion

        #region 结束事务

        /// <summary>
        /// 结束事务
        /// </summary>
        [MethodImpl(MethodImplOptions.Synchronized)]
        private void EndTransaction()
        {
            if (tran != null && IsTranEnded == false)
            {
                if (this.IsSuccess)
                    tran.Commit();
                else
                    tran.Rollback();

                //标记结束
                IsTranEnded = true;
            }
        }

        #endregion


        #region 标记执行成功

        public void MarkSuccess()
        {
            this.IsSuccess = true;
        }

        #endregion
    }

}
